package com.zang.dp;

/**
 * @author ZVerify
 * @since 2022/11/24 16:57
 * @see <a href="https://leetcode.cn/problems/unique-paths-ii/description/">...</a>
 **/
public class 不同路径II {

    public static void main(String[] args) {
        不同路径II ii = new 不同路径II();
        ii.uniquePathsWithObstacles(new int[][]{{0,0,0},{0,1,0},{0,0,0,}});
    }

    public int uniquePathsWithObstacles(int[][] obstacleGrid) {

        int m = obstacleGrid.length;
        int n = obstacleGrid[0].length;
        // 个人习惯多出一行一列
        int[][] dp = new int[m+1][n+1];

        // 如果开始或者结尾有障碍物说明肯定不能到达
        if (obstacleGrid[0][0] == 1 || obstacleGrid[m-1][n-1]==1) return 0;
        // 初始化
        for (int i = 1; i < n; i++) {
            // 如果遇到障碍物直接设置为0，后边到不了就还是0
            if (obstacleGrid[0][i]==1) {
                dp[0][i] = 0;
                break;
            }
            dp[0][i] = 1;
        }
        for (int i = 1; i < m; i++) {
            if (obstacleGrid[i][0]==1) {
                dp[i][0] = 0;
                break;
            }
            dp[i][0] = 1;
        }
        for (int i = 1; i < m; i++) {
            for (int j = 1; j < n; j++) {
                // 如果为障碍物直接将他设为0
                if (obstacleGrid[i][j]==1){
                    dp[i][j] = 0;
                    continue;
                }
                dp[i][j] = dp[i-1][j]+dp[i][j-1];
                System.out.println(dp[i][j]);
            }
        }
        return dp[m-1][n-1];
    }

}
